<template>
  <div style="width: 100%">
    <div style="width: 100%">
      <div
        id="linezhewailiu"
        style="width: 100%; height: 200px; top: -15px"
      ></div>
    </div>
  </div>
</template>

<script>
import { showYear } from '@/utils/index.js'
export default {
  components: {},
  props: {
    data: [],
    grid: {
      default: () => {
        return {
          top: "25%",
          left: "10%",
          right: "5%",
          bottom: "15%",
        };
      },
    },
  },
  data() {
    return {
      dataArr : [],
    };
  },
  watch: {},
  computed: {},
  methods: {
    init(data) {
      let xdata = [];
      let dataval = [];
      // data.forEach((item) => {
      //   xdata.push(item.yearMonth);
      //   dataval.push(item.cigarNum);
      // });
      xdata=this.dataArr
      this.dataArr.forEach(time=>{
       let k= data.find(it=>it.yearMonth==time)
      //  console.log(k);
       if(k&&k.cigarNum){
        dataval.push(k.cigarNum=='-'?0: parseFloat(k.cigarNum));
       }else{
        dataval.push(0);
       }
      })
      console.log('data数据',data);
      let min = data.sort((a, b) => a.cigarNum - b.cigarNum)[0]?.cigarNum;
      let max = data.sort((a, b) => a.cigarNum - b.cigarNum)[data.length - 1]?.cigarNum;
      console.log(min, "min");
      console.log(max, "max");

      let mychart = this.$echarts.init(
        document.getElementById("linezhewailiu")
      );
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(0, 255, 233,0)",
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255, 255, 255,1)",
                  },
                  {
                    offset: 1,
                    color: "rgba(0, 255, 233,0)",
                  },
                ],
                global: false,
              },
            },
          },
        },
        grid: this.grid,
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            axisLine: {
              lineStyle: {
                color: "white",
              },
            },
            // 刻度标记
            axisLabel: {
              // interval: 0,
              // rotate: 40,
              textStyle: {
                fontFamily: "Microsoft YaHei",
              },
              // align: 'right',
              // 增加刻度靠右
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.3)",
              },
            },
            data: xdata,
          },
        ],

        yAxis: [
          {
            name: "单位：(万支)",
            type: "value",
            // min: min,
            // max: max,
            // splitNumber: 4,
            position: "left",
            axisLine: {
              // y轴线
              show: false,
              lineStyle: {
                color: "white",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.3)",
              },
            },
            axisLabel: {
              show: true,
              margin: 20,
              textStyle: {
                color: "#fff",
              },
            },
            // y轴单位标注点
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: "",
            type: "line",
            showAllSymbol: true,
            symbol: "circle",
            symbolSize: 10,
            lineStyle: {
              normal: {
                color: "#2CABE3",
              },
            },
            label: {
              show: false,
              position: "top",
              textStyle: {
                color: "#2CABE3",
              },
            },
            itemStyle: {
              color: "#fff",
              borderColor: "#2CABE3",
              borderWidth: 2,
            },
            areaStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(81,150,164,0.3)",
                    },
                    {
                      offset: 1,
                      color: "rgba(81,150,164,0)",
                    },
                  ],
                  false
                ),
              },
            },
            data: dataval,
          },
        ],
      };

      mychart.setOption(option);
    },
  },
  created() {
    this.dataArr = showYear()
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>